//
//  MSDetailSituationTableVC.m
//  SocietySale
//
//  Created by Mike on 16/11/8.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSDetailSituationTableVC.h"
#import "MSDetailInfoCell.h"
#import "MSDetailDynamicCell.h"

@interface MSDetailSituationTableVC ()
//section头部标题
@property(nonatomic,copy)NSArray *sectionTitleArray;
//项目信息数据
@property(nonatomic,copy)NSArray *projectInfoArray;
@end

@implementation MSDetailSituationTableVC

- (void)setModel:(MSProjectModel *)model{
    _model = model;

    self.sectionTitleArray = @[@"项目信息",@"项目动态"];
    
    self.projectInfoArray = @[[DOKeyValue key:@"项目名称" value:model.title],
                                [DOKeyValue key:@"项目区域" value:model.projectArea],
                                [DOKeyValue key:@"投资金额" value:model.investment],
                                [DOKeyValue key:@"项目阶段" value:model.projectState],
                                [DOKeyValue key:@"发布时间" value:model.time],
                                [DOKeyValue key:@"建设周期" value:model.projectPeriod],
                                [DOKeyValue key:@"项目地点" value:model.address],
                                [DOKeyValue key:@"项目简介" value:model.introduction],
                              ];
}

- (void)setProjectDynamicArray:(NSArray *)projectDynamicArray{
    _projectDynamicArray = projectDynamicArray;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {//信息
        static NSString *identifier = @"InfoCell";
        MSDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[MSDetailInfoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.delegate = (id)self.delegate;
        DOKeyValue *dataValue = self.projectInfoArray[indexPath.row];
        cell.data = dataValue;
        if (indexPath.row == self.projectInfoArray.count-1) {
            CGSize size = [HDUtil sizeWithString:dataValue.value font:[UIFont systemFontOfSize:14] size:CGSizeMake(SCREEN_WIDTH-90, CGFLOAT_MAX)];
            cell.horizonLineView.frame = CGRectMake(0, size.height+26-0.6, SCREEN_WIDTH, 0.6);
            if (self.isClick) {//点击过
                if (size.height>350) {
                    cell.horizonLineView.top = size.height+66-0.6;
                }
                else{
                    cell.horizonLineView.top = size.height+36-0.6;
                }
                
            }
            else{//未点击过更多
                if (size.height>60) {// 大于3行  68.05
                    cell.horizonLineView.top = 68+16-0.6;
                }
            }
        }
        else{
            CGSize size = [HDUtil sizeWithString:dataValue.value font:[UIFont systemFontOfSize:14] size:CGSizeMake(SCREEN_WIDTH-90, CGFLOAT_MAX)];
            if (size.height<30) {
                cell.horizonLineView.frame = CGRectMake(12, 40-0.6, SCREEN_WIDTH-24, 0.6);
            }
            else if (size.height>30 && size.height<40) {//  两行  33
                cell.horizonLineView.frame = CGRectMake(12, size.height+26-0.6, SCREEN_WIDTH-24, 0.6);
            }
            else if (size.height>40 && size.height<60) {//  3行  50.05
                cell.horizonLineView.frame = CGRectMake(12, 68+16-0.6, SCREEN_WIDTH-24, 0.6);
            }
            else{
                cell.horizonLineView.frame = CGRectMake(12, size.height+36-0.6, SCREEN_WIDTH-24, 0.6);
            }
        }
        return cell;
    }
    else if(indexPath.section == 1){//动态
        static NSString *identifier = @"DynamicCell";
        MSDetailDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[MSDetailDynamicCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.model = self.projectDynamicArray[indexPath.row];
        if (indexPath.row == self.projectDynamicArray.count-1) {
            cell.verticalLineView.height = 30;
        }
        return cell;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {//信息
        return self.projectInfoArray.count;
    }
    else if(section == 1){//动态
        return self.projectDynamicArray.count;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {//信息
        if (self.isClick) {
            return [MSDetailInfoCell cellHeight:self.projectInfoArray[indexPath.row]];
        }
        else{//未点击过更多
            DOKeyValue *data = self.projectInfoArray[indexPath.row];
            CGSize size = [HDUtil sizeWithString:data.value font:[UIFont systemFontOfSize:14] size:CGSizeMake(SCREEN_WIDTH-90, CGFLOAT_MAX)];
            if (size.height>60) {// 大于3行  68.05    
                return 68+16;
            }
        }
        return [MSDetailInfoCell cellHeight:self.projectInfoArray[indexPath.row]];
    }
    else if(indexPath.section == 1){//动态
        return [MSDetailDynamicCell cellHeightWithModel:self.projectDynamicArray[indexPath.row]];
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {//信息
        return 35;
    }
    else if(section == 1){//动态
        return 55;
    }
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 35)];
    bgView.backgroundColor = kDefaultViewBackgroundColor;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 150, 35)];
    titleLabel.text = self.sectionTitleArray[section];
    titleLabel.textColor = kDarkGrayColor;
    titleLabel.font = HeitiFont(15);
    [bgView addSubview:titleLabel];
    
    if(section == 1){//动态
        if (self.projectDynamicArray.count) {
            titleLabel.text = @"项目动态";
            UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, 35, SCREENWIDTH, 20)];
            whiteView.backgroundColor = [UIColor whiteColor];
            [bgView addSubview:whiteView];
        }
        else{
            titleLabel.text = @"项目动态（暂无）";
        }
    }
    return bgView;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if ([self.delegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
        [self.delegate performSelector:@selector(scrollViewWillBeginDragging:) withObject:self];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
